﻿@model InvoiceModel
@using Telerik.Web.Mvc.UI;
@using Nop.Core.Domain.Tax;
@using Nop.Core.Domain.Catalog;
@using Nop.Core.Domain.Orders


@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.OrderIds)
@Html.HiddenFor(model => model.StartDate)
@Html.HiddenFor(model => model.EndDate)

@{
    //page title
    ViewBag.Title = T("Admin.Invoice.EditDetails").Text;
}
@if (!string.IsNullOrEmpty(ViewBag.ErrorMessage))
{
    <div>@ViewBag.ErrorMessage</div>
}
else
{
    using (Html.BeginForm(null, null, FormMethod.Post, new { id = "invoice-form" }))
    {
        <div class="section-header">
            <div class="title">
                <img src="@Url.Content("~/subsites/Administration/Content/images/ico-sales.png")" alt="" />
                @T("Admin.Invoice.EditDetails") - @Model.Id @Html.ActionLink("(" + T("Admin.Invoice.BackToList") + ")", "List")
            </div>
            <div class="options">
                @if (Model.InvoiceStatusId == (int)Nop.Core.Domain.Orders.InvoiceStatus.Pending)
                {
                    <input type="submit" name="publish" value="@T("Admin.Invoice.Publish")" id="publish" class="t-button" />
                }
                <a href="@Url.Action("PdfInvoice", new { invoiceId = Model.Id })" class="t-button">@T("Admin.Orders.PdfInvoice")</a>
                <input type="submit" name="save" class="t-button" value="@T("Admin.Common.Save")" />
                <input type="submit" name="save-continue" class="t-button" value="@T("Admin.Common.SaveContinue")" />
                <span id="invoice-delete" class="t-button">@T("Admin.Common.Delete")</span>
                <input type="submit" name="sendexpress" value="@T("Admin.Invoice.SendInvoiceNotification")" id="sendexpress" class="t-button" />
            </div>
        </div>


        @Html.Telerik().TabStrip().Name("invoice-edit").Items(x =>
    {
        var tabInfo = x.Add().Text(T("Admin.Invoice.Info").Text).Content(TabInfo().ToHtmlString());
        var tabInvoiceInfo = x.Add().Text(T("Admin.Orders").Text).Content(TabInvoiceInfo().ToHtmlString());
        var tabInvoiceNotes = x.Add().Text(T("Admin.Invoice.InvoiceNotes").Text).Content(TabInvoiceNotes().ToHtmlString());

        var selectedTab = (string)ViewData["selectedTab"];
        switch (selectedTab)
        {
            case "info":
                tabInfo.Selected(true);
                break;
            case "invoiceinfo":
                tabInvoiceInfo.Selected(true);
                break;
            case "invoicenotes":
                tabInvoiceNotes.Selected(true);
                break;
            default:
                tabInfo.Selected(true);
                break;
        }

        //generate an event
        EngineContext.Current.Resolve<IEventPublisher>().Publish(new AdminTabStripCreated(x, "invoice-edit"));
    })

    }
    @Html.DeleteConfirmation("invoice-delete")


    @helper TabInfo()
{
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.InvoiceStatus):
            </td>
            <td class="adminData">
                <table>
                    <tr>
                        <td class="adminData">
                            <strong>
                                @Html.DisplayFor(model => model.InvoiceStatus)
                            </strong>
                        </td>

                        @if (Model.InvoiceStatusId == (int)Nop.Core.Domain.Orders.InvoiceStatus.Published)
                        {
                            <td class="adminData">
                                <input type="submit" name="setordersaspaid" value="@T("Admin.Invoice.SetAsPaid")" id="setordersaspaid" class="adminButton" />
                            </td>

                            <td class="adminData">
                                <table>

                                    @foreach (var method in Model.AvailablePaymentMethods)
                                    {
                                        <tr>
                                            <td>
                                                @Html.RadioButton("PaymentMethodId", method.Value, method.Selected)
                                                <label class="forcheckbox" for=@method.Value>
                                                    @method.Text
                                                    @if (Model.PaymentMethodId.HasValue && Model.PaymentMethodId.Value.ToString() == method.Value)
                                                    {
                                                        <text>(*)</text>
                                                    }
                                                </label>
                                            </td>
                                        </tr>
                                    }
                                </table>
                            </td>
                        }
                        @if (Model.CanChangeStatus)
                        {
                            <td class="adminData">
                                @Html.DropDownListFor(model => model.InvoiceStatusId, Model.AvailableStatuses)
                            </td>
                        }
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Id):
            </td>
            <td class="adminData">
                @Model.Id
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.InvoiceGuid):
            </td>
            <td class="adminData">
                @Model.InvoiceGuid
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.InvoiceNumber):
            </td>
            <td class="adminData">
                @if (Model.CanEditInvoiceNumber)
                {
                    @Html.EditorFor(model => model.InvoiceNumber)
                }
                else
                {
                    @Model.InvoiceNumber
                }
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CreatedOn):
            </td>
            <td class="adminData">
                @Model.CreatedOn
            </td>
        </tr>

        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CustomerId):
            </td>
            <td class="adminData">
                @Html.DropDownList("CustomerId", Model.AvailableCustomers)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Name):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DueDate):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DueDate)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.OrderKindStr):
            </td>
            <td class="adminData">
                @Html.DisplayFor(model => model.OrderKindStr)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.TimeZoneId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.TimeZoneId, Model.AvailableTimeZones)
            </td>
        </tr>
        @if (Model.OrderKindId == (int)Nop.Core.Domain.Orders.OrderKind.Commerce)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.MonthlyCharge):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(model => model.MonthlyCharge)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.StorageCharge):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(model => model.StorageCharge)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.ServiceCharge):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(model => model.ServiceCharge)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.TransactionProcessingCharge):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(model => model.TransactionProcessingCharge)
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SubTotal):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SubTotal)
            </td>
        </tr>
        @if (Model.FuelSurcharge.HasValue)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.FuelSurcharge):
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.FuelSurcharge)
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Tax):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Tax)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Total):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Total)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Discount):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Discount)
            </td>
        </tr>
    </table>
}
    @helper TabInvoiceInfo()
{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
    <table class="adminContent">
        <tr>
            <td>
                @* Hiding grid table's refresh button *@
                @Html.Telerik().ScriptRegistrar().OnDocumentReady("$('#orders-grid .t-status').hide();")

                @(Html.Telerik().Grid(Model.Orders)
                    .Name("orders-grid")
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.Id);
                        columns.Bound(x => x.OrderStatus);
                        columns.Bound(x => x.PaymentStatus);
                        columns.Bound(x => x.ShippingStatus);
                        columns.Bound(x => x.CustomerEmail);
                        columns.Bound(x => x.CreatedOn).ClientTemplate("<#= CreatedOnStr #>");
                        columns.Bound(x => x.OrderTotal)
                                .FooterTemplate(
                                string.Format("<div><strong>{0}:</strong></div>" +
                                "<div>{1}: <span id=\"aggregator-subtotal-block\">{5}</span></div>" +
                                "<div>{2}: <span id=\"aggregator-fuelsurcharge-block\">{6}</span></div>" +
                            //"<div>{1}: <span id=\"aggregator-profit-block\">{4}</span></div>" +
                                "<div>{3}: <span id=\"aggregator-tax-block\">{7}</span></div>" +
                                "<div>{4}: <span id=\"aggregator-total-block\">{8}</span></div>",
                                @T("Admin.Orders.Report.Summary").Text,
                                @T("Admin.Orders.Report.SubtotalExclTax").Text,
                                @T("Admin.Orders.Report.FuelSurcharge").Text,
                            //@T("Admin.Orders.Report.Profit").Text,
                                @T("Admin.Orders.Report.Tax").Text,
                                @T("Admin.Orders.Report.Total").Text,
                            //@Model.AggregatorProfit,
                                @Model.AggregatorSubtotalExcltax,
                                @Model.AggregatorFuelSurcharge,
                                @Model.AggregatorTax,
                                @Model.AggregatorTotal));
                    })
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both)))
            </td>
        </tr>
    </table>
}

    @helper TabInvoiceNotes()
{
    {
        <table class="adminContent">
            <tr>
                <td>
                    @(Html.Telerik().Grid<InvoiceModel.InvoiceNote>().Name("invoicenotes-grid")
                        .DataKeys(keys =>
                        {
                            keys.Add(x => x.Id).RouteKey("invoiceNoteId");
                            keys.Add(x => x.InvoiceId).RouteKey("invoiceId");
                        })
                        .DataBinding(binding =>
                        {
                            binding.Ajax().Select("InvoiceNotesSelect", "Invoice", new { invoiceId = Model.Id });
                        })
                        .Columns(columns =>
                        {
                            columns.Bound(x => x.CreatedOn).ClientTemplate("<#= CreatedOnStr #>").Width(100);
                            columns.Bound(x => x.Note).Encoded(false).Width(500);
                            columns.Bound(x => x.DisplayToCustomer).Width(100);
                        })
                        .EnableCustomBinding(true))
                </td>
            </tr>
        </table>
        <div class="clear">
        </div>
        <p>
            <strong>
                @T("Admin.Invoice.InvoiceNotes.AddTitle")
            </strong>
        </p>
        <table class="adminContent">
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.AddInvoiceNoteMessage)
                </td>
                <td class="adminData">
                    @Html.TextAreaFor(model => model.AddInvoiceNoteMessage, new { style = "Width: 350px; Height: 150px;" })
                    @Html.ValidationMessageFor(model => model.AddInvoiceNoteMessage)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.AddInvoiceNoteDisplayToCustomer)
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.AddInvoiceNoteDisplayToCustomer)
                    @Html.ValidationMessageFor(model => model.AddInvoiceNoteDisplayToCustomer)
                </td>
            </tr>
            <tr>
                <td colspan="2" align="left">
                    <input type="button" id="addInvoiceNote" class="t-button" value="@T("Admin.Invoice.InvoiceNotes.AddButton")" />
                </td>
            </tr>
        </table>

        <script type="text/javascript">
            $(document).ready(function () {
                $('#addInvoiceNote').click(function () {
                    var invoiceNoteMessage = $("#@Html.FieldIdFor(model => model.AddInvoiceNoteMessage)").val();
                    var invoiceNoteDisplayToCustomer = $("#@Html.FieldIdFor(model => model.AddInvoiceNoteDisplayToCustomer)").is(':checked');
                    if (!invoiceNoteMessage)
                        return;
                    $('#addInvoiceNote').attr('disabled', true);
                    $.ajax({
                        cache:false,
                        type: "POST",
                        url: "@(Url.Action("InvoiceNoteAdd", "Invoice"))",
                        data: { "DisplayToCustomer": invoiceNoteDisplayToCustomer, "message": invoiceNoteMessage, "invoiceId": @Model.Id },
                        success: function (data) {
                            var invoicenotesGrid = $("#invoicenotes-grid");
                            invoicenotesGrid.data('tGrid').ajaxRequest();
                            $('#addInvoiceNote').attr('disabled', false);
                        },
                        error:function (xhr, ajaxOptions, thrownError){
                            alert('Failed to add invoice note.');
                            $('#addInvoiceNote').attr('disabled', false);
                        }
                    });
                });
            });
        </script>
    }

}

}



